#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QKeyEvent>
#include<QTimer>
#include"snake.h"
#include<QMessageBox>
#include<QLabel>
#include<QPushButton>
#include<QPixmap>
#include<QtMultimedia/QMediaPlayer>
#include<QMediaPlaylist>
#include<QPropertyAnimation>

QMediaPlaylist *eat=new QMediaPlaylist;
QMediaPlaylist *bgmplaylist=new QMediaPlaylist;
QMediaPlayer* player=new QMediaPlayer;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{


    ui->setupUi(this);

    //窗口初始化
    setFixedSize(1000,600);
    setWindowIcon(QIcon(":/res/icon.jpg"));


    //背景音乐
    bgmplaylist->addMedia(QUrl("qrc:/res/bgm.mp3"));
    bgmplaylist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
    eat->addMedia(QUrl("qrc:/res/eat.mp3"));
    eat->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
    player->setPlaylist(bgmplaylist);
    player->play();


    QPixmap tmppix;
    tmppix.load(":/res/go.jpg");
    winlabel->setGeometry(0,0,tmppix.width(),tmppix.height());
    winlabel->setPixmap(tmppix);
    winlabel->setParent(this);
    winlabel->move(150,-300);


    //速度滑条
    ui->horizontalSlider->setValue(200>>2);
    connect(ui->horizontalSlider,QSlider::valueChanged,[=](){
       timerspeed=ui->horizontalSlider->value()<<2;
    });

    ispause=false;
    connect(timer,SIGNAL(timeout()),this,SLOT(snakeupdate()));

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::gameover()
{
    timer->stop();
    QPropertyAnimation * animation = new QPropertyAnimation(winlabel,"geometry");
    animation->setDuration(0);
    animation->setStartValue(QRect(winlabel->x(),winlabel->y(),winlabel->width(),winlabel->height()));
    animation->setEndValue(QRect(winlabel->x(),winlabel->y()+450,winlabel->width(),winlabel->height()));
    animation->start();
    time->stop();
}


void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    //背景
    p.setBrush(Qt::green);
    p.drawRect(0,0,600,600);
    //背景2
    QPixmap bgp;
    bgp.load(":/res/background.png");
    p.drawPixmap(625,0,375,600,bgp);
    //墙
    p.setBrush(Qt::gray);
    p.drawRect(600,0,25,600);
    //得分
    p.setPen(Qt::black);
    p.setFont(QFont("Arial",20));
    p.drawText(760,100,"得分："+QString::number(core));
    //画食物
   QPixmap food;
   food.load(":/res/food.jpg");
   p.drawPixmap(getfoodx(),getfoody(),25,25,food);
    //画蛇
    QPixmap headw;
    headw.load(":/res/snakehead(w).jpg");
    QPixmap heads;
    heads.load(":/res/snakehead(s).jpg");
    QPixmap heada;
    heada.load(":/res/snakehead(a).jpg");
    QPixmap headd;
    headd.load(":/res/snakehead(d).jpg");
    QPixmap body;
    body.load(":/res/body.jpg");
    for(auto snakenodei:snake.snakevec)
    {
        if(snakenodei.x==snake.snakevec.at(0).x&&snakenodei.y==snake.snakevec.at(0).y)
        {
            switch (key)
            {
            case'w':
                p.drawPixmap(snakenodei.x,snakenodei.y,25,25,headw);
                break;
            case's':
                p.drawPixmap(snakenodei.x,snakenodei.y,25,25,heads);
                break;
            case'a':
                p.drawPixmap(snakenodei.x,snakenodei.y,25,25,heada);
                break;
            case'd':
                p.drawPixmap(snakenodei.x,snakenodei.y,25,25,headd);
                break;
            default:
                break;
            }
        }
        else
            p.drawPixmap(snakenodei.x,snakenodei.y,25,25,body);
    }
    bugflag=false;
}
void MainWindow::keyPressEvent(QKeyEvent *ev)
{
    if(bugflag)
        return;
    switch (ev->key())
    {
    case Qt::Key_W:
        if(key=='s')
            break;
        key='w';
        bugflag=true;
        break;
    case Qt::Key_S:
        if(key=='w')
            break;
        key='s';
        bugflag=true;
        break;
    case Qt::Key_A:
        if(key=='d')
            break;
        key='a';
        bugflag=true;
        break;
    case Qt::Key_D:
        if(key=='a')
            break;
        key='d';
        bugflag=true;
        break;
    default:
        timer->start(timerspeed);
    }
}
bool MainWindow::isgameover()
{
    if(snake.snakevec.at(0).x>=24*25||snake.snakevec.at(0).x<0||snake.snakevec.at(0).y>=24*25||snake.snakevec.at(0).y<0)//撞墙
    {
        return true;
    }
    for(int i=1;i<snake.snakevec.size();i++)//自己吃到自己
    {
        if(snake.snakevec.at(0).x==snake.snakevec.at(i).x&&snake.snakevec.at(0).y==snake.snakevec.at(i).y)
        {
            return true;
        }
    }
    return false;
}

void MainWindow::on_pushButton_clicked()//重新开始按钮
{
    winlabel->move(150,-300);
    ui->horizontalSlider->setValue(200>>2);
    snake.reset();
    core=0;
    key='d';
    timerspeed=200;
    timer->start(timerspeed);
    isgameover();
}

void MainWindow::on_pushButton_2_clicked()//暂停按钮
{
    ispause=!ispause;
    if(ispause)
    {
        timer->stop();
    }
    else
    {
        timer->start(timerspeed);
    }
}


void MainWindow::on_pushButton_3_clicked()//开始按钮
{
    start=true;
    if(start)
        timer->start(timerspeed);
}

void MainWindow::creatfood()//产生食物
{
    srand((unsigned)time(NULL));
    food_x=(rand()%24)*25;
    food_y=(rand()%24)*25;
    for(int i=0;i<snake.snakevec.size();i++)
    {
        if(getfoodx()==snake.snakevec[i].x&&getfoody()==snake.snakevec[i].y)
            creatfood();
    }
}

void MainWindow::snakeupdate()
{
    timer->start(timerspeed);
    snake.move(key);
    if(getfoodx()==snake.snakevec.at(0).x&&getfoody()==snake.snakevec.at(0).y)
    {
        QMediaPlayer* playere=new QMediaPlayer;
        playere->setPlaylist(eat);
        playere->play();
        core++;
        creatfood();
    }
    else
    {
        snake.snakevec.pop_back();
    }
    if(isgameover())
    {
        gameover();
        return;
    }
    update();
}

void MainWindow::on_pushButton_4_clicked()
{
    bgmcls=!bgmcls;
    if(bgmcls)
        player->stop();
    else
        player->play();
}
